//
//  HLIMKitMessageModel.m
//  YueYue
//
//  Created by hulu001 on 2024/1/11.
//

#import "HLIMKitMessageModel.h"
#import "HLIMCellLayoutConfig.h"
#import "HLChatKit.h"

@interface HLIMKitMessageModel ()

@property (nonatomic,strong) NSMutableDictionary *contentSizeInfo;
@property (nonatomic,strong) NSMutableDictionary *replyContentSizeInfo;

@end

@implementation HLIMKitMessageModel

@synthesize contentViewInsets  = _contentViewInsets;
@synthesize bubbleViewInsets   = _bubbleViewInsets;
@synthesize replyContentViewInsets  = _replyContentViewInsets;
@synthesize replyBubbleViewInsets   = _replyBubbleViewInsets;
@synthesize shouldShowAvatar   = _shouldShowAvatar;
@synthesize shouldShowLeft     = _shouldShowLeft;
@synthesize shouldShowTime     = _shouldShowTime;
@synthesize avatarMargin       = _avatarMargin;
@synthesize nickNameMargin     = _nickNameMargin;
@synthesize avatarSize         = _avatarSize;
@synthesize messageContentName = _messageContentName;
@synthesize messageTime        = _messageTime;

- (instancetype)initWithMessage:(NIMMessage *)message
{
    if (self = [self init])
    {
        _message = message;
        _enableRepliedContent = YES;
        _isRemoteRead = message.isRemoteRead;
        _contentSizeInfo = [NSMutableDictionary dictionary];
    }
    return self;
}

- (NSString*)description{
    return @"聊天内容";
}

- (CGSize)contentSize:(CGFloat)width
{
    CGSize size = [self.contentSizeInfo[@(width)] CGSizeValue];
    if (CGSizeEqualToSize(size, CGSizeZero))
    {
        [self updateLayoutConfig];
        id<HLIMCellLayoutConfig> layoutConfig = [HLChatKit.shareSDK cellConfig];
        size = [layoutConfig contentSize:self cellWidth:width];
        [self.contentSizeInfo setObject:[NSValue valueWithCGSize:size] forKey:@(width)];
    }
    return size;
}

- (UIEdgeInsets)contentViewInsets{
    if (UIEdgeInsetsEqualToEdgeInsets(_contentViewInsets, UIEdgeInsetsZero))
    {
        id<HLIMCellLayoutConfig> layoutConfig = [HLChatKit.shareSDK cellConfig];
        _contentViewInsets = [layoutConfig contentViewInsets:self];
    }
    return _contentViewInsets;
}

- (UIEdgeInsets)bubbleViewInsets{
    if (UIEdgeInsetsEqualToEdgeInsets(_bubbleViewInsets, UIEdgeInsetsZero))
    {
        id<HLIMCellLayoutConfig> layoutConfig = [HLChatKit.shareSDK cellConfig];
        _bubbleViewInsets = [layoutConfig cellInsets:self];
    }
    return _bubbleViewInsets;
}

- (CGSize)replyContentSize:(CGFloat)width
{
    id<HLIMCellLayoutConfig> layoutConfig = [HLChatKit.shareSDK cellConfig];
    CGSize size = [layoutConfig replyContentSize:self cellWidth:width];
    return size;
}

- (UIEdgeInsets)replyContentViewInsets{
    if (UIEdgeInsetsEqualToEdgeInsets(_replyContentViewInsets, UIEdgeInsetsZero))
    {
        id<HLIMCellLayoutConfig> layoutConfig = [HLChatKit.shareSDK cellConfig];
        _replyContentViewInsets = [layoutConfig replyContentViewInsets:self];
    }
    return _replyContentViewInsets;
}

- (UIEdgeInsets)replyBubbleViewInsets{
    if (UIEdgeInsetsEqualToEdgeInsets(_replyBubbleViewInsets, UIEdgeInsetsZero))
    {
        id<HLIMCellLayoutConfig> layoutConfig = [HLChatKit.shareSDK cellConfig];
        _replyBubbleViewInsets = [layoutConfig replyCellInsets:self];
    }
    return _replyBubbleViewInsets;
}

- (void)cleanCache
{
    _contentViewInsets = UIEdgeInsetsZero;
    _bubbleViewInsets = UIEdgeInsetsZero;
    [_contentSizeInfo removeAllObjects];
}

- (void)updateLayoutConfig
{
    id<HLIMCellLayoutConfig> layoutConfig = [HLChatKit.shareSDK cellConfig];
    
    _shouldShowAvatar       = [layoutConfig shouldShowAvatar:self];
    _shouldShowLeft         = [layoutConfig shouldShowLeft:self];
    _avatarMargin           = [layoutConfig avatarMargin:self];
    _nickNameMargin         = [layoutConfig nickNameMargin:self];
    _avatarSize             = [layoutConfig avatarSize:self];
    _shouldShowTime         = [layoutConfig shouldShowTime:self];
}

- (BOOL)needShowRepliedContent
{
    BOOL should = self.message.messageType == NIMMessageTypeTip;
    return !should && self.enableRepliedContent &&
    self.message.repliedMessageId.length > 0;
}

- (NSTimeInterval)messageTime
{
    return _message.timestamp;
}

- (BOOL)shouldShowUnRead
{
    NIMMessageType type = self.message.messageType;
    return (type == NIMMessageTypeText || type == NIMMessageTypeImage || type == NIMMessageTypeAudio || type == NIMMessageTypeVideo) && self.message.isOutgoingMsg;
}


@end
